home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc2 / fiflb382.lha / lib.c < prev    next >
C/C++ Source or Header  |  1996-05-13  |  3KB  |  124 lines

  1.  
  2. /*
  3.  *  LIB.C
  4.  *
  5.  *  Basic Library Resource Handling
  6.  *
  7.  *  NOTE: all data declarations should be initialized since we skip
  8.  *      normal C startup code (unless initial value is don't care)
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. #ifndef _DCC
  14. extern const LONG (*Vectors[])(); /*  Assembler library entry points */
  15. #else
  16. extern LONG Vectors();          /*  trick a reference in the code section */
  17.                   /*  __far would require the registered DICE */
  18. #endif
  19.  
  20. /*  Avoid initializations so we save a data segment */
  21. long    SegList;
  22. #ifdef _DCC    /*  work around pc-relative bug in DICE-2.06.xx */
  23. ExecBase * SysBase  = NULL;
  24. #else
  25. ExecBase * SysBase;
  26. #endif
  27. List    FifoList;
  28.  
  29. /*
  30.  *    The Initialization routine is given only a seglist pointer.  Since
  31.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  32.  *    and return either NULL or the library pointer.  Exec has Forbid()
  33.  *    for us during the call.
  34.  *
  35.  *    If you have an extended library structure you must specify the size
  36.  *    of the extended structure in MakeLibrary().
  37.  */
  38.  
  39. LibCall Library *
  40. LibInit(long segment)
  41. {
  42.     Library *lib;
  43.  
  44.     SysBase = *((ExecBase **) 4L);
  45.  
  46.     if ((lib = MakeLibrary(Vectors,NULL,NULL,sizeof(Library),NULL))) {
  47.     lib->lib_Node.ln_Type = NT_LIBRARY;
  48.     lib->lib_Node.ln_Name = (char *)LibName;
  49.     lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  50.     lib->lib_Version  = 38;
  51.     lib->lib_Revision = 2;
  52.     lib->lib_IdString = (APTR)LibId;
  53.     SegList = segment;
  54.     AddLibrary(lib);
  55.  
  56.     NewList((MaxList *)&FifoList);
  57.     }
  58.     return(lib);
  59. }
  60.  
  61. /*
  62.  *    We expunge the library and return the Seglist ONLY if the open count
  63.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  64.  *    flag and return NULL.
  65.  *
  66.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  67.  *    might be called from the memory allocator and thus we CANNOT DO A
  68.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  69.  *
  70.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  71.  *    therefore freeze if we called it ourselves.  As far as I can tell
  72.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  73.  *    below.
  74.  */
  75.  
  76. LibCall long
  77. LibExpunge(long dummy, Library *lib)
  78. {
  79.     if (lib->lib_OpenCnt) {
  80.     lib->lib_Flags |= LIBF_DELEXP;
  81.     return(NULL);
  82.     }
  83.     Remove((MaxNode *)lib);
  84.     FreeMem((void *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  85.  
  86.     return(SegList);
  87. }
  88.  
  89. /*
  90.  *    Open is given the library pointer and the version request.  Either
  91.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  92.  *    Exec has Forbid() for us during the call.
  93.  */
  94.  
  95. LibCall Library *
  96. LibOpen(long version, Library *lib)
  97. {
  98.     ++lib->lib_OpenCnt;
  99.     lib->lib_Flags &= ~LIBF_DELEXP;
  100.     return(lib);
  101. }
  102.  
  103. /*
  104.  *    Close is given the library pointer and the version request.  Be sure
  105.  *    not to decrement the open count if already zero.    If the open count
  106.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  107.  *    and return the seglist.  Otherwise we return NULL.
  108.  *
  109.  *    Note that this routine never sets LIBF_DELEXP on its own.
  110.  *
  111.  *    Exec has Forbid() for us during the call.
  112.  */
  113.  
  114. LibCall long
  115. LibClose(long dummy, Library *lib)
  116. {
  117.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  118.     return(NULL);
  119.     if (lib->lib_Flags & LIBF_DELEXP)
  120.     return(LibExpunge(0, lib));
  121.     return(NULL);
  122. }
  123.  
  124.